package modules.createplayer
{
import core.StageProxy;
import core.model.SystemModel;

import framework.proxy.ComponentProxy;
import framework.view.components.button.ButtonProxy;
import framework.view.components.container.IRenderProxy;
import framework.view.components.container.RepeaterProxy;

import modules.createplayer.value.CreatePlayerAvatarValue;
import modules.createplayer.value.CreatePlayerSettingValue;

public class CreatePlayerUIProxy extends ComponentProxy
{
    //==========================================================================
    //  Constructor
    //==========================================================================
	public function CreatePlayerUIProxy()
	{
		super();
	}
    //==========================================================================
    //  Properties
    //==========================================================================
    protected function get list():RepeaterProxy
    {
        return proxys["list"];
    }
    
    protected function get startBtn():ButtonProxy
    {
        return proxys["startBtn"];
    }
    //==========================================================================
    //  Properties
    //==========================================================================
	[Inject]
	public var systemModel:SystemModel;
    
    protected var selectedAvatar:String;
    
    protected var _data:CreatePlayerSettingValue;

    public function get data():CreatePlayerSettingValue
    {
        return _data;
    }

    public function set data(value:CreatePlayerSettingValue):void
    {
        _data = value;
        
        list.data = data.avatarList.sortOn("index", Array.NUMERIC);
    }
    //==========================================================================
    //  Overridden methods: ComponentProxy
    //==========================================================================
	override protected function get proxyMapping():Array
	{
		return [ 
            { skin:"", classRef: RepeaterProxy, id:"list", renderProxyRef:CreateAvatarItemRenderProxy, onItemSelected:onSelectHandler },
            { skin:"startBtn", classRef: ButtonProxy, id:"startBtn", onClick: onClickToStartHandler }
        ];
	}
    //==========================================================================
    //  Event handlers
    //==========================================================================
	protected function onClickToStartHandler():void
	{
		if (selectedAvatar == null) return;
        
        StageProxy.lock();
        systemModel.initPlayer(selectedAvatar);
	}
    
    protected function onSelectHandler(render:IRenderProxy):void
    {
        selectedAvatar = CreatePlayerAvatarValue(render.data).avatarKey;
    }
}
}




